<?php
include 'functions.inc';
$all_users = arrayfile_to_array('./user_data.dat');
include 'product_data.inc';


//shows the invoice if register is pressed and data is valid.
if (array_key_exists('register', $_POST)) {
    // assume registration data is valid, unless otherwise shown.
    $errors = false;
    //checks the parameters.
    $username = $_POST["username"];
    if (!strlen($username)) {
        echo "Please enter username<br/>";
        $errors = true;
    }

    $password = $_POST["password"];
    if (!strlen($password)) {
        echo "Please enter password<br/>";
        $errors = true;
    }

    $confirm_password = $_POST["confirm_password"];
    if (!strlen($confirm_password)) {
        echo "Please enter password in confirm password<br/>";
        $errors = true;
    }

    $email = $_POST["email"];
    if (!strlen($email)) {
        echo "Please enter your Email address<br/>";
        $errors = true;
    }

    if ($confirm_password == $password) {
        //correct
    } else {
        echo 'Confirm password has to match password<br/>';
        $errors = true;
    }


    //make sure that entered value is within the range
    if (strlen($username) <= 3) {
        echo "Username has to be minimum of 4 characters and maximum of 11 characters<br/>";
        $errors = true;
    }

    if (strlen($username) >= 12) {
        echo "Username has to be minimum of 4 characters and maximum of 11 characters<br/>";
        $errors = true;
    }

    if (strlen($password) <= 5) {
        echo "Password has to be at least 6 characters<br/>";
        $errors = true;
    }

    //check the email address is valid
    if (preg_match('/^[-+.\\w]+@[-a-z0-9]+(\\.[-a-z0-9]+)*\\.[a-z]{2,6}$/i', $email)) {
        //valid email address
    } else {
        //invalid
        echo 'Please enter valid email address</br>';
        $errors = true;
    }
    
    //check if email is already registered
    if(array_key_exists($email,$all_users)){
        echo 'This email is already taken.';
        $errors = true;
    }

    //check if username is already registered
    if(array_key_exists($username,$all_users)){
        echo 'This username is already taken.';
        $errors = true;
    }
    //check if username only consists of letters and numbers. 
    if (preg_match("/^[a-zA-Z0-9]+$/", $username)) {

        //ok
    } else {
        echo 'Only letters and numbers are valid for username</br>';
        $errors = true;
    }

    //If the user data is valid, add the new user to the data file, and print invoice.
    if (!$errors) {
        $new_user = array(
            'firstName' => $_POST['firstName'],
            'lastName' => $_POST['lastName'],
            'email' => $email,
            'username' => $username,
            'password' => $password
        );
        $all_users[$username] = $new_user;
        array_to_arrayfile($all_users, './user_data.dat');

        print "<form action='invoice.php' method=post>";
        for ($i = 0; $i < count($products); $i++) {
            $qty = $_POST["quantity$i"];
            print "<input type=hidden name=quantity$i value=$qty>";
        }
        echo 'Thank you for registering, ' . $_POST['firstName'] .'.';
        echo "<br>";
        echo 'Please wait a moment for the system to process your information.';
        echo "<br>";
        echo "<input type=submit value='Click here to continue to your shopping cart'>";
        exit();
    }
}
    //Create registration form 
    ?>
        <form action='<?= $_SERVER['PHP_SELF'] ?>' method="post">
        First Name: <br>
        <?php //This needs to be sticky if the form returns an error.  ?>
        <input type="text" name="firstName" size="11" value="<?php if (isset($_POST['firstName'])) echo $_POST['firstName'] ?>" minlength="4" maxlength=”11"><br />
        Last Name: <br>
        <?php //This needs to be sticky if the form returns an error.  ?>
        <input type="text" name="lastName" size="11" value="<?php if (isset($_POST['lastName'])) echo $_POST['lastName'] ?>" minlength="4" maxlength=”11"><br />
        Username(minimum of 4 characters and maximum of 11 characters):<br />
        <?php //This needs to be sticky if the form returns an error.  ?>
        <input type="text" name="username" size="11" value="<?php if (isset($_POST['username'])) echo $_POST['username'] ?>" minlength="4" maxlength=”11"><br />
        Password(minimum of 6 characters):<br />
        <input type="password" name="password" size="30" value="" minlength="6"/><br />
        Confirm password:<br />
        <input type="password" name="confirm_password" size="30" value="" minlength="6"><br />
        Email address:<br />
        <?php //This needs to be sticky if the form returns an error.  ?>
        <input type="text" name="email" size="30" value="<?php if (isset($_POST['email'])) echo $_POST['email'] ?>"><br />
        <br />
        <input type="submit" name="register" value="Register">
        <?php
        // this will take the quantity data from the original post and keep it while the user tries to login
        for ($i = 0; $i < count($products); $i++) {
            $qty = $_POST["quantity$i"];
            print "<input type=hidden name=quantity$i value=$qty>";
        }
        ?>
        </form>